//Programs TABLE8A and TABLE8B produce the results in TABLE 8.
//The programs are intended to be run in tandem, with program A run first.
//It takes approximately 24 hours on a laptop to run 1,000 iterations.

etime, start
log using "\\file\UsersW$\wrr15\Home\My Documents\My Files\HURWICZ BIAS\REVISION FOR ECONOMIC MODELLING\PROGRAMS2\TABLE8.smcl", replace
drop _all
clear
graph drop _all
set more off
set seed 13

matrix AHmedLRP = J(5,4,0)
matrix AHmse = J(5,4,0)
matrix AHmeanRR = J(5,4,0)

matrix DGMMmedLRP = J(5,4,0)
matrix DGMMmse = J(5,4,0)
matrix DGMMmeanRR = J(5,4,0)

matrix SGMMmedLRP = J(5,4,0)
matrix SGMMmse = J(5,4,0)
matrix SGMMmeanRR = J(5,4,0)

matrix MG1medLRP = J(5,4,0)
matrix MG1mse = J(5,4,0)
matrix MG1meanRR = J(5,4,0)

matrix CCEMGmedLRP = J(5,4,0)
matrix CCEMGmse = J(5,4,0)
matrix CCEMGmeanRR = J(5,4,0)

matrix AMGmedLRP = J(5,4,0)
matrix AMGmse = J(5,4,0)
matrix AMGmeanRR = J(5,4,0)

matrix DFEmedLRP = J(5,4,0)
matrix DFEmse = J(5,4,0)
matrix DFEmeanRR = J(5,4,0)

matrix MG2medLRP = J(5,4,0)
matrix MG2mse = J(5,4,0)
matrix MG2meanRR = J(5,4,0)

matrix DOLSmedLRP = J(5,4,0)
matrix DOLSmse = J(5,4,0)
matrix DOLSmeanRR = J(5,4,0)

local numN = 50 // This sets the number of cross-sectional units
local numT = 40 // This sets the number of time observations per unit
local beta0 = 0 // This sets the intercept term
local betax = 1 // This sets the slope coefficient for x


local b = 0
foreach betay in 0.60 0.90 {
   local truLRP = `betax'/(1-`betay')
   local i = 1
   foreach rhox in 0 0.5 0.9 0.95 1 {
      simulate AHLRP25 = r(AHLRP25) pAHLRP25 = r(pAHLRP25) AHLRP5 = r(AHLRP5) pAHLRP5 = r(pAHLRP5) ///
	     DGMMLRP25 = r(DGMMLRP25) pDGMMLRP25 = r(pDGMMLRP25) DGMMLRP5 = r(DGMMLRP5) pDGMMLRP5 = r(pDGMMLRP5)  /// 
		 SGMMLRP25 = r(SGMMLRP25) pSGMMLRP25 = r(pSGMMLRP25) SGMMLRP5 = r(SGMMLRP5) pSGMMLRP5 = r(pSGMMLRP5)  ///
		 MG1LRP25 = r(MG1LRP25) pMG1LRP25 = r(pMG1LRP25) MG1LRP5 = r(MG1LRP5) pMG1LRP5 = r(pMG1LRP5)  ///
		 CCEMGLRP25 = r(CCEMGLRP25) pCCEMGLRP25 = r(pCCEMGLRP25) CCEMGLRP5 = r(CCEMGLRP5) pCCEMGLRP5 = r(pCCEMGLRP5)  ///
		 AMGLRP25 = r(AMGLRP25) pAMGLRP25 = r(pAMGLRP25) AMGLRP5 = r(AMGLRP5) pAMGLRP5 = r(pAMGLRP5)  ///
		 DFELRP25 = r(DFELRP25) pDFELRP25 = r(pDFELRP25) DFELRP5 = r(DFELRP5) pDFELRP5 = r(pDFELRP5)  ///
		 MG2LRP25 = r(MG2LRP25) pMG2LRP25 = r(pMG2LRP25) MG2LRP5 = r(MG2LRP5) pMG2LRP5 = r(pMG2LRP5)  ///
		 DOLSLRP25 = r(DOLSLRP25) pDOLSLRP25 = r(pDOLSLRP25)  , ///
		 reps(1000): TABLE8prog, betay(`betay') truLRP(`truLRP') ///
		 beta0(`beta0') betax(`betax') rhox(`rhox') numN(`numN') numT(`numT')
	 
      summ AHLRP25, detail
      matrix AHmedLRP[`i',1+(`b'*2)] = r(p50)
	  generate mseAHLRP25 = (AHLRP25 - `truLRP')^2
	  summ mseAHLRP25, meanonly
	  matrix AHmse[`i',1+(`b'*2)] = r(mean)
	  generate RejectRate = 0
      replace RejectRate = 1 if pAHLRP25 < 0.05  
	  summ RejectRate, meanonly
      matrix AHmeanRR[`i',1+(`b'*2)] = r(mean)
	 	  
      summ AHLRP5, detail
      matrix AHmedLRP[`i',2+(`b'*2)] = r(p50)
	  generate mseAHLRP5 = (AHLRP5 - `truLRP')^2
	  summ mseAHLRP5, meanonly
	  matrix AHmse[`i',2+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pAHLRP5 < 0.05  
	  summ RejectRate, meanonly
      matrix AHmeanRR[`i',2+(`b'*2)] = r(mean)
		 
      summ DGMMLRP25, detail
      matrix DGMMmedLRP[`i',1+(`b'*2)] = r(p50)
	  generate mseDGMMLRP25 = (DGMMLRP25 - `truLRP')^2
	  summ mseDGMMLRP25, meanonly
	  matrix DGMMmse[`i',1+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pDGMMLRP25 < 0.05  
	  summ RejectRate, meanonly
      matrix DGMMmeanRR[`i',1+(`b'*2)] = r(mean)
	  
      summ DGMMLRP5, detail
      matrix DGMMmedLRP[`i',2+(`b'*2)] = r(p50)
	  generate mseDGMMLRP5 = (DGMMLRP5 - `truLRP')^2
	  summ mseDGMMLRP5, meanonly
	  matrix DGMMmse[`i',2+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pDGMMLRP5 < 0.05  
	  summ RejectRate, meanonly
      matrix DGMMmeanRR[`i',2+(`b'*2)] = r(mean)
		 
      summ SGMMLRP25, detail
      matrix SGMMmedLRP[`i',1+(`b'*2)] = r(p50)
	  generate mseSGMMLRP25 = (SGMMLRP25 - `truLRP')^2
	  summ mseSGMMLRP25, meanonly
	  matrix SGMMmse[`i',1+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pSGMMLRP25 < 0.05  
	  summ RejectRate, meanonly
      matrix SGMMmeanRR[`i',1+(`b'*2)] = r(mean)
	  
      summ SGMMLRP5, detail
      matrix SGMMmedLRP[`i',2+(`b'*2)] = r(p50)
	  generate mseSGMMLRP5 = (SGMMLRP5 - `truLRP')^2
	  summ mseSGMMLRP5, meanonly
	  matrix SGMMmse[`i',2+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pSGMMLRP5 < 0.05  
	  summ RejectRate, meanonly
      matrix SGMMmeanRR[`i',2+(`b'*2)] = r(mean)
		 
      summ MG1LRP25, detail
      matrix MG1medLRP[`i',1+(`b'*2)] = r(p50)
	  generate mseMG1LRP25 = (MG1LRP25 - `truLRP')^2
	  summ mseMG1LRP25, meanonly
	  matrix MG1mse[`i',1+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pMG1LRP25 < 0.05  
	  summ RejectRate, meanonly
      matrix MG1meanRR[`i',1+(`b'*2)] = r(mean)
	  
      summ MG1LRP5, detail
      matrix MG1medLRP[`i',2+(`b'*2)] = r(p50)
	  generate mseMG1LRP5 = (MG1LRP5 - `truLRP')^2
	  summ mseMG1LRP5, meanonly
	  matrix MG1mse[`i',2+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pMG1LRP5 < 0.05  
	  summ RejectRate, meanonly
      matrix MG1meanRR[`i',2+(`b'*2)] = r(mean)
		 
      summ CCEMGLRP25, detail
      matrix CCEMGmedLRP[`i',1+(`b'*2)] = r(p50)
	  generate mseCCEMGLRP25 = (CCEMGLRP25 - `truLRP')^2
	  summ mseCCEMGLRP25, meanonly
	  matrix CCEMGmse[`i',1+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pCCEMGLRP25 < 0.05  
	  summ RejectRate, meanonly
      matrix CCEMGmeanRR[`i',1+(`b'*2)] = r(mean)
	  
      summ CCEMGLRP5, detail
      matrix CCEMGmedLRP[`i',2+(`b'*2)] = r(p50)
	  generate mseCCEMGLRP5 = (CCEMGLRP5 - `truLRP')^2
	  summ mseCCEMGLRP5, meanonly
	  matrix CCEMGmse[`i',2+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pCCEMGLRP5 < 0.05  
	  summ RejectRate, meanonly
      matrix CCEMGmeanRR[`i',2+(`b'*2)] = r(mean)
		 
      summ AMGLRP25, detail
      matrix AMGmedLRP[`i',1+(`b'*2)] = r(p50)
	  generate mseAMGLRP25 = (AMGLRP25 - `truLRP')^2
	  summ mseAMGLRP25, meanonly
	  matrix AMGmse[`i',1+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pAMGLRP25 < 0.05  
	  summ RejectRate, meanonly
      matrix AMGmeanRR[`i',1+(`b'*2)] = r(mean)
	  
      summ AMGLRP5, detail
      matrix AMGmedLRP[`i',2+(`b'*2)] = r(p50)
	  generate mseAMGLRP5 = (AMGLRP5 - `truLRP')^2
	  summ mseAMGLRP5, meanonly
	  matrix AMGmse[`i',2+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pAMGLRP5 < 0.05  
	  summ RejectRate, meanonly
      matrix AMGmeanRR[`i',2+(`b'*2)] = r(mean)
		 
      summ DFELRP25, detail
      matrix DFEmedLRP[`i',1+(`b'*2)] = r(p50)
	  generate mseDFELRP25 = (DFELRP25 - `truLRP')^2
	  summ mseDFELRP25, meanonly
	  matrix DFEmse[`i',1+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pDFELRP25 < 0.05  
	  summ RejectRate, meanonly
      matrix DFEmeanRR[`i',1+(`b'*2)] = r(mean)
	  
      summ DFELRP5, detail
      matrix DFEmedLRP[`i',2+(`b'*2)] = r(p50)
	  generate mseDFELRP5 = (DFELRP5 - `truLRP')^2
	  summ mseDFELRP5, meanonly
	  matrix DFEmse[`i',2+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pDFELRP5 < 0.05  
	  summ RejectRate, meanonly
      matrix DFEmeanRR[`i',2+(`b'*2)] = r(mean)
		 
      summ MG2LRP25, detail
      matrix MG2medLRP[`i',1+(`b'*2)] = r(p50)
	  generate mseMG2LRP25 = (MG2LRP25 - `truLRP')^2
	  summ mseMG2LRP25, meanonly
	  matrix MG2mse[`i',1+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pMG2LRP25 < 0.05  
	  summ RejectRate, meanonly
      matrix MG2meanRR[`i',1+(`b'*2)] = r(mean)
	  
      summ MG2LRP5, detail
      matrix MG2medLRP[`i',2+(`b'*2)] = r(p50)
	  generate mseMG2LRP5 = (MG2LRP5 - `truLRP')^2
	  summ mseMG2LRP5, meanonly
	  matrix MG2mse[`i',2+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pMG2LRP5 < 0.05  
	  summ RejectRate, meanonly
      matrix MG2meanRR[`i',2+(`b'*2)] = r(mean)
		 
      summ DOLSLRP25, detail
      matrix DOLSmedLRP[`i',1+(`b'*2)] = r(p50)
	  generate mseDOLSLRP25 = (DOLSLRP25 - `truLRP')^2
	  summ mseDOLSLRP25, meanonly
	  matrix DOLSmse[`i',1+(`b'*2)] = r(mean)
	  replace RejectRate = 0
      replace RejectRate = 1 if pDOLSLRP25 < 0.05  
	  summ RejectRate, meanonly
      matrix DOLSmeanRR[`i',1+(`b'*2)] = r(mean)	  
	  
   local `++i'
   }
local `++b'
} 



matrix colnames AHmedLRP = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames AHmedLRP= R00 R50 R90 R95 R100
    
matrix colnames AHmse =  "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames AHmse= R00 R50 R90 R95 R100
    
matrix colnames AHmeanRR = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames AHmeanRR = R00 R50 R90 R95 R100
    
   
************************************************
************************************************
************   TABLE 8A: AH RESULTS   **********
************************************************
************************************************

matrix list AHmedLRP
matrix list AHmse
matrix list AHmeanRR

matrix colnames DGMMmedLRP = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames DGMMmedLRP= R00 R50 R90 R95 R100
matrix colnames DGMMmse =  "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames DGMMmse = R00 R50 R90 R95 R100
matrix colnames DGMMmeanRR = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames DGMMmeanRR = R00 R50 R90 R95 R100
   
************************************************
************************************************
**********   TABLE 8B: DGMM RESULTS   **********
************************************************
************************************************

matrix list DGMMmedLRP
matrix list DGMMmse
matrix list DGMMmeanRR

matrix colnames SGMMmedLRP = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames SGMMmedLRP= R00 R50 R90 R95 R100
    
matrix colnames SGMMmse =  "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames SGMMmse= R00 R50 R90 R95 R100
    
matrix colnames SGMMmeanRR = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames SGMMmeanRR = R00 R50 R90 R95 R100
    
   
************************************************
************************************************
**********   TABLE 8C: SGMM RESULTS   **********
************************************************
************************************************

matrix list SGMMmedLRP
matrix list SGMMmse
matrix list SGMMmeanRR

matrix colnames MG1medLRP = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames MG1medLRP= R00 R50 R90 R95 R100
    
matrix colnames MG1mse =  "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames MG1mse= R00 R50 R90 R95 R100
    
matrix colnames MG1meanRR = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames MG1meanRR = R00 R50 R90 R95 R100
    
   
************************************************
************************************************
**********   TABLE 8D: MG1 RESULTS   **********
************************************************
************************************************

matrix list MG1medLRP
matrix list MG1mse
matrix list MG1meanRR

matrix colnames CCEMGmedLRP = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames CCEMGmedLRP= R00 R50 R90 R95 R100
    
matrix colnames CCEMGmse =  "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames CCEMGmse= R00 R50 R90 R95 R100
    
matrix colnames CCEMGmeanRR = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames CCEMGmeanRR = R00 R50 R90 R95 R100
    
   
************************************************
************************************************
**********   TABLE 8E: CCEMG RESULTS   **********
************************************************
************************************************

matrix list CCEMGmedLRP
matrix list CCEMGmse
matrix list CCEMGmeanRR

matrix colnames AMGmedLRP = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames AMGmedLRP= R00 R50 R90 R95 R100
    
matrix colnames AMGmse =  "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames AMGmse= R00 R50 R90 R95 R100
    
matrix colnames AMGmeanRR = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames AMGmeanRR = R00 R50 R90 R95 R100
    
   
************************************************
************************************************
**********   TABLE 8F: AMG RESULTS   **********
************************************************
************************************************

matrix list AMGmedLRP
matrix list AMGmse
matrix list AMGmeanRR

matrix colnames DFEmedLRP = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames DFEmedLRP= R00 R50 R90 R95 R100
    
matrix colnames DFEmse =  "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames DFEmse= R00 R50 R90 R95 R100
    
matrix colnames DFEmeanRR = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames DFEmeanRR = R00 R50 R90 R95 R100
    
   
************************************************
************************************************
**********   TABLE 8G: DFE RESULTS   **********
************************************************
************************************************

matrix list DFEmedLRP
matrix list DFEmse
matrix list DFEmeanRR

matrix colnames MG2medLRP = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames MG2medLRP= R00 R50 R90 R95 R100
    
matrix colnames MG2mse =  "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames MG2mse= R00 R50 R90 R95 R100
    
matrix colnames MG2meanRR = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames MG2meanRR = R00 R50 R90 R95 R100
    
   
************************************************
************************************************
**********   TABLE 8H: MG2 RESULTS   **********
************************************************
************************************************

matrix list MG2medLRP
matrix list MG2mse
matrix list MG2meanRR

matrix colnames DOLSmedLRP = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames DOLSmedLRP= R00 R50 R90 R95 R100
    
matrix colnames DOLSmse =  "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames DOLSmse= R00 R50 R90 R95 R100
    
matrix colnames DOLSmeanRR = "BY60:T25" "BY60:T5" "BY90:T25" "BY90:T5"
matrix rownames DOLSmeanRR = R00 R50 R90 R95 R100
    
   
************************************************
************************************************
**********   TABLE 8I: DOLS RESULTS   **********
************************************************
************************************************

matrix list DOLSmedLRP
matrix list DOLSmse
matrix list DOLSmeanRR

etime

log close
